<?php
include('../includes/common.php');
if ($islogin2 == 1) { } else exit("<script language='javascript'>window.location.href='./login.php';</script>");
?>

<?php
$my = isset($_GET['my']) ? $_GET['my'] : null;
if ($my == 'add') {
    $active = 'dormlist-add';
    $title = '添加宿舍';
} else {
    $active = 'dormlist';
    $title = '宿舍列表';
}
include('head.php');
?>
<section id="main-content">
    <section class="wrapper">
        <div class="modal fade" align="left" id="search" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">搜索宿舍</h4>
                    </div>
                    <div class="modal-body">
                        <input type="text" class="form-control" name="kw" placeholder="请输入宿舍"><br />
                        <button type="button" class="btn btn-primary btn-block" id="search_submit">搜索</button>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" align="left" id="up_url" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">一键修改</h4>
                    </div>
                    <div class="modal-body">
                        <input type="text" class="form-control" name="ckurl" placeholder="请输入宿舍"><br />
                        <button type="button" class="btn btn-primary btn-block" id="update_submit">修改</button>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
        <?php

        $uid = $userrow['id'];
        if ($my == 'add') {
            ?>
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            添加新宿舍
                        </header>
                        <div class="panel-body">
                            <form action="./dormlist.php?my=add_submit" method="post" class="cmxform form-horizontal tasi-form" role="form">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">宿舍名:</label>
                                    <div class="col-sm-10"><input type="text" name="name" value="" class="form-control" required /></div>
                                </div><br />
								<div class="form-group">
                                    <label class="col-sm-2 control-label">现有舍费:</label>
                                    <div class="col-sm-10"><input type="text" name="money" value="" class="form-control" /></div>
                                </div><br />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注:</label>
                                    <div class="col-sm-10"><input type="text" name="remarks" value="" class="form-control" /></div>
                                </div><br />
                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-danger" type="submit">确认添加</button>
                                        <a href="./dormlist.php" class="btn btn-default" type="button">返回列表</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>
                </div>
            </div>
        <?php
        } elseif ($my == 'add_submit') {
            $name = trim($_POST['name']);
            $money = trim($_POST['money']);
            $remarks = $_POST['remarks'];
            if (($uid == NULL || $name == NULL)) {
                showmsg('保存错误,请确保每项都不为空!', 3);
            } else {
                $sql = "insert into dorm_list(uid,addtime,name,money,remarks) values('$uid','$nowDate','$name','$money','$remarks')";
                $rs = $conn->query($sql);
                if ($rs) {
                    showmsg('添加宿舍成功！<br/>宿舍名 ' . $name . '<br/>所属UID ' . $uid . '<br/><br/><a href="./dormlist.php">>>返回宿舍列表</a>', 1);
                } else {
					echo $sql;
                    showmsg('添加宿舍失败！', 4);
                }
            }
        } elseif ($my == 'edit_submit') {
            $id = $_GET['id'];
            $rows = $conn->query("select * from dorm_list where id = '{$id}'");
			$res1 = $rows->fetch_assoc();
            if ($rows->num_rows == 0) {
                showmsg('当前记录不存在！', 3);
            } else {
				if($res1['state'] == 0){
						showmsg('该宿舍已被封禁', 4);
					}else{
                $name = trim($_POST['name']);
                $money = trim($_POST['money']);
                $remarks = $_POST['remarks'];
                if (($name == NULL)) {
                    showmsg('保存错误,请确保每项都不为空!', 3);
                } else {
                    $rs = $conn->query("update dorm_list set uid='$uid',name='$name',money='$money',remarks='$remarks' where id='$id'");
                    if ($rs) {
                        showmsg('修改宿舍成功！<br/><br/><a href="./dormlist.php">>>返回宿舍列表</a>', 1);
                    } else {
                        showmsg('修改宿舍失败！', 4);
                    }
                }
            }}
        } elseif ($my == 'edit') {
            $id = $_GET['id'];
            $rs = $conn->query("select * from dorm_list where id= '$id' ");
            if ($rs->num_rows == 0) {
                exit("<script language='javascript'>alert('当前记录不存在');history.go(-1);</script>");
            }
            $row = $rs->fetch_assoc();
            ?>
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            修改网址信息
                        </header>
                        <div class="panel-body">
                            <form action="./dormlist.php?my=edit_submit&id=<?php echo $row['id']; ?>" method="post" class="cmxform form-horizontal tasi-form" role="form">
                                

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">宿舍名:</label>
                                    <div class="col-sm-10"><input type="text" name="name" value="<?php echo $row['name']; ?>" class="form-control" /></div>
                                </div><br />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">舍费:</label>
                                    <div class="col-sm-10"><input type="text" name="money" value="<?php echo $row['money']; ?>" class="form-control" /></div>
                                </div><br />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注:</label>
                                    <div class="col-sm-10"><input type="text" name="remarks" value="<?php echo $row['remarks']; ?>" class="form-control" />
                                        <p class="help-block">可留空</p>
                                    </div>
                                </div><br />
								
								
                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-danger" type="submit">确认修改</button>
                                        <a href="./dormlist.php" class="btn btn-default" type="button">返回列表</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>
                </div>
            </div>
        <?php
        } elseif ($my == 'delete') {
            $id = $_GET['id'];
            $rs = $conn->query("select * from dorm_list where id = '$id' and uid = '$uid'");
            if ($rs->num_rows == 0) {
                showmsg('当前记录不存在！', 3);
            } else {
                
					$rs = $conn->query("delete from dorm_list where id = '$id'");
                    showmsg('删除成功！<br/><br/><a href="./dormlist.php">>>返回宿舍列表</a>', 1);
                } 
            }
         else {
            ?>
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            <a href="./dormlist.php?my=add" class="btn btn-primary">添加宿舍</a>&nbsp;
                            <div class="btn-group">
                                <a class="btn btn-warning" href="#" data-toggle="dropdown">
                                    操作
                                    <i class="icon-angle-down "></i>
                                </a>
                                <ul class="dropdown-menu">
                                    <li><a href="#" onclick="datadel()"><i class="icon-trash"></i>一键删除</a></li>
                                    <li><a href="#" data-toggle="modal" data-target="#up_url" id="up_url"><i class="icon-pencil"></i>一键修改</a></li>
                                </ul>
                            </div>
                            <a href="javascript:listTable('start')" class="btn btn-default" title="刷新宿舍列表"><i class="fa fa-refresh"></i></a>
                        </header>
                        <div id="listTable"></div>
                    </section>
                </div>
            </div>
        <?php
        }
        ?>
    </section>
</section>
<script src="../static/user/js/common-scripts.js"></script>
<script>
    function listTable(query) {
        var url = window.document.location.href.toString();
        var queryString = url.split("?")[1];
        query = query || queryString;
        if (query == 'start' || query == undefined) {
            query = '';
            history.replaceState({}, null, './dormlist.php');
        } else if (query != undefined) {
            history.replaceState({}, null, './dormlist.php?' + query);
        }
        layer.closeAll();
        var ii = layer.load(2, {
            shade: [0.1, '#fff']
        });
        $.ajax({
            type: 'GET',
            url: 'dormlist-table.php?' + query,
            dataType: 'html',
            cache: false,
            success: function(data) {
                layer.close(ii);
                $("#listTable").html(data)
            },
            error: function(data) {
                layer.msg('服务器错误');
                return false;
            }
        });
    }

    function datadel() {
        layer.confirm('您确定要删除这些宿舍吗？', {
            btn: ['确定', '取消']
        }, function() {
            var cks = document.getElementsByName("checkid");
            var str = "";
            for (var i = 0; i < cks.length; i++) {
                if (cks[i].checked) {
                    str += cks[i].value + "&";
                }
            }
            str = str.substring(0, str.length - 1);
            $.ajax({
                url: "ajax.php?act=delSelect",
                type: "POST",
                data: {
                    "str": str
                },
                dataType: "json",
                success: function(result) {
                    if (result.code == 0) {
                        window.location.reload();
                    }
                },
                error: function(data) {
                    layer.msg('服务器错误');
                    return false;
                }
            });
        })
    }

    function show(id) {
        $.ajax({
            type: 'GET',
            url: 'ajax.php?act=getInfo&id=' + id,
            dataType: 'json',
            success: function(data) {
                if (data.code == 0) {
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-molv',
                        anim: 2,
                        shadeClose: true,
                        title: '详细信息',
                        content: '<div style="padding:15px"><p>SID：' + data.id + '</p><p>UID：' + data.uid + '</p><p>宿舍名：' + data.name + '</p><p>余额：' + data.money + '</p><p>状态：' + data.state + '</p><p>添加时间：' + data.addtime + '</p><p>备注：' + data.remarks + '</p></div>'
                    });
                } else {
                    layer.alert(data.msg);
                }
            },
            error: function(data) {
                layer.msg('服务器错误');
                return false;
            }
        });
    }

    $(document).ready(function() {
        $("#search_submit").click(function() {
            var kw = $("input[name='kw']").val();
            if (kw == '') {
                listTable('start');
            } else {
                listTable('kw=' + kw);
            }
        });
        $("#update_submit").click(function() {
            var url = $("input[name='ckurl']").val();
            $("#up_url").modal('hide');
            layer.confirm('您确定要修改这些宿舍的名字吗？', {
                btn: ['确定', '取消']
            }, function() {
                var cks = document.getElementsByName("checkid");
                var str = "";
                for (var i = 0; i < cks.length; i++) {
                    if (cks[i].checked) {
                        str += cks[i].value + "&";
                    }
                }
                str = str.substring(0, str.length - 1);
                $.ajax({
                    url: "ajax.php?act=upSelect&name=" + url,
                    type: "POST",
                    data: {
                        "str": str
                    },
                    dataType: "json",
                    success: function(result) {
                        if (result.code == 0) {
                            window.location.reload();
                        }
                    },
                    error: function(data) {
                        layer.msg('服务器错误');
                        return false;
                    }
                });
            })
        });
    });

    $(document).ready(function() {
        listTable();
    })
</script>